package com.zixuan.gulicollege.cmsservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zixuan.gulicollege.cmsservice.service.CrmBannerService;
import com.zixuan.gulicollege.common_utils.PageUtils;
import com.zixuan.gulicollege.common_utils.R;
import com.zixuan.gulicollege.entity.cms.CrmBanner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName: CrmBannerAdminController
 * @Author: zixuan
 * @Date: 2021/5/16 14:36
 * @Description:
 */
@RestController
@RequestMapping("/cms_service/bannerAdmin")
public class CrmBannerAdminController {

    @Autowired
    private CrmBannerService bannerService;


    /**
     * 分页查询banner
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/pageBanner/{current}/{limit}")
    public R pageBanner(@PathVariable Integer current, @PathVariable Integer limit) {
        Page<CrmBanner> pageBanner = new Page<>(current, limit);
        Map<String, Object> map = PageUtils.pageInfo(pageBanner, null, bannerService);
        return R.ok().data(map);
    }

    /**
     * 添加banner
     * @param banner
     * @return
     */
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner banner) {
        bannerService.save(banner);
        return R.ok();
    }

    /**
     * 修改banner
     * @param banner
     * @return
     */
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    /**
     * 删除banner
     * @param bannerId
     * @return
     */
    @DeleteMapping("/deleteBanner/{bannerId}")
    public R deleteBanner(@PathVariable String bannerId) {
        bannerService.removeById(bannerId);
        return R.ok();
    }

    /**
     * 根据id查询banner
     * @param id
     * @return
     */
    @GetMapping("/getBanner/{id}")
    public R getBanner(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("banner", banner);
    }

}
